package org.zjt.book

import java.io._
import java.net.MalformedURLException

import scala.io.BufferedSource

/**
  * Created by Administrator on 2017/4/29.
  */
object FileTest {

  def main(args: Array[String]): Unit = {

    /**
      * 读取文件
      */
    def readFile = {
      var file: BufferedSource = null
      try {
        file = scala.io.Source.fromFile("test.txt", "utf-8")
        for (line <- file.getLines()) {
          println(line.mkString)
        }
      } catch {
        case e => e.printStackTrace()
      } finally {
        if (file != null) file.close()
      }
    }

    readFile


    /**
      * 远程调用的到数据
      */
    def readUrl = {
      var content: BufferedSource = null
      try {
        content = scala.io.Source.fromURL("https://www.baidu.com", "utf-8")
        for (line <- content.getLines()) {
          println(line.mkString)
        }
      } catch {
        case _: MalformedURLException => println("url is error")
      } finally {
        if (content != null) content.close()
      }
    }

    readUrl


    /**
      * string 格式化
      * @param str
      * @return
      */
    def formatMoney(str: Float): String = {
      "%.2f".format(str)
    }

    println(formatMoney(0.1F))



    /**
      * 递归得到文件路径
      * @param path
      * @return
      */

    def listdirs(path: String): Iterator[File] = {
      try {
        val file = new File(path)
        val children = file.listFiles().filter(_.isDirectory)
        children.toIterator ++ children.map(_.getPath).flatMap(listdirs _)
      } catch {
        case e => {
          e.printStackTrace();
          println("出现错误" + path)
        }
          null
      }

    }

    println(listdirs("D:\\Idea workspace\\spring-boot-starter").mkString("\n"))


    /**
      * 讀取文件
      * @param content
      */
    def writeFile(content: String) = {
      val file = new File("./zhang.txt")
      var outPrint: FileOutputStream = null
      try {
        outPrint = new FileOutputStream(file)
        outPrint.write(content.getBytes)
      } finally {
        if (outPrint != null) outPrint.close()
      }
      outPrint.close()
    }

    writeFile(listdirs("D:\\Idea workspace\\scala-demo").mkString("\n"))


    /**
      * 对象的序列化和反序列化
      *
      * @param person
      * @return
      */
    def serialPerson(person: PersonSerial): String = {
      var out = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(new File("./person.txt"))))
      out.writeObject(person)
      out.close()
      "./person.txt"
    }

    def readObject(path: String): Unit = {

      var in = new ObjectInputStream(new FileInputStream(new File(path)))
      var person = in.readObject().asInstanceOf[PersonSerial];

      println(person.age)
    }

    serialPerson(new PersonSerial("ZHANGSAN", 18))

    readObject("./person.txt")


    /**
      * 传递函数，科里函数
      *
      * @param var1
      * @param var2
      * @return
      */
    def keli(var1: Int)(var2: Int): Int = var1 + var2

    println(keli(2)(201))


    /**
      * 等价于柯里函数
      *
      * @param var1
      * @return
      */
    def keliEquals(var1: Int) = { (var2: Int) => var1 + var2 }

    println(keliEquals(2)(201))


  }

}


@SerialVersionUID(1L)
class PersonSerial(var name: String, var age: Int, var id: Int = 1) extends Serializable {
  override def toString: String = super.toString
}
